package com.ruoyi.project.wxuser.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.wxuser.domain.UserUpdate;
import com.ruoyi.project.wxuser.service.IUserUpdateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 更新审核Controller
 * 
 * @author wyh
 * @date 2024-06-14
 */
@RestController
@RequestMapping("/wxuser/updateUser/api")
public class UserUpdateApiController extends BaseController
{
    @Autowired
    private IUserUpdateService userUpdateService;


    /**
     * 获取更新审核详细信息
     */
    @PreAuthorize("@ss.hasPermi('wxuser:updateUser:query')")
    @GetMapping(value = "/{userId}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userUpdateService.selectUserUpdateById(id));
    }

    /**
     * 新增更新审核
     */
    @PreAuthorize("@ss.hasPermi('wxuser:updateUser:add')")
    @Log(title = "更新审核", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserUpdate userUpdate)
    {
        //校验所有信息不为空
        if (userUpdate.getBrith() == null
                && userUpdate.getHeight() == null
                && userUpdate.getWeight() == null
                && userUpdate.getCityCode() == null
                && userUpdate.getOccupation() == null
                && userUpdate.getEmail() == null
                && userUpdate.getNickName() == null
                && userUpdate.getAvatar() == null) {
            return error("请填写修改的信息");
        }

        Long userId = SecurityUtils.getUserId();
        userUpdate.setUserId(userId);
        userUpdate.setStatus("待审核");

        //校验身高
        Double height = userUpdate.getHeight();
        if (height != null) {
            if(height >= 50 && height <= 300){
                return error("请输入有效身高");
            }
        }

        //校验体重
        Double weight = userUpdate.getWeight();
        if (weight != null) {
            if(weight >= 20 && weight <= 300){
                return error("请输入有效体重");
            }
        }

        return toAjax(userUpdateService.insertUserUpdate(userUpdate));
    }

    /**
     * 修改更新审核
     */
    @PreAuthorize("@ss.hasPermi('wxuser:updateUser:edit')")
    @Log(title = "更新审核", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserUpdate userUpdate)
    {
        return toAjax(userUpdateService.updateUserUpdate(userUpdate));
    }

}
